package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.domain.AdminPrizeRecord;
import com.ruoyi.system.domain.AdminUserBetRecord;
import com.ruoyi.system.domain.AdminWeixinUser;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.AdminAgentInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 开奖设置Controller
 *
 * @author ruoyi
 * @date 2020-12-16
 */
@RestController
@RequestMapping("/system/agentState")
public class AdminAgentStateController extends BaseController
{
    @Autowired
    private IAdminAgentInfoService adminAgentInfoService;

    @Autowired
    private IAdminUserFlowingService iAdminUserFlowingService;

    @Autowired
    private IAdminPrizeRecordService iAdminPrizeRecordService;

    @Autowired
    private IAdminUserBetRecordService iAdminUserBetRecordService;

    @Autowired
    private IAdminWeixinUserService iAdminWeixinUserService;

    @Value("${send.url}")
    private String sendUrl;

    /**
     * 查询开奖设置列表
     */
    @PreAuthorize("@ss.hasPermi('system:agentState:list')")
    @GetMapping("/list")
    public TableDataInfo list(AdminAgentInfo adminAgentInfo)
    {
        startPage();
        String room = SecurityUtils.getLoginUser().getUser().getRoom();
        adminAgentInfo.setRoom(room);
        List<AdminAgentInfo> list = adminAgentInfoService.selectAdminAgentInfoList(adminAgentInfo);
        return getDataTable(list);
    }

    /**
     * 导出开奖设置列表
     */
    @PreAuthorize("@ss.hasPermi('system:agentState:export')")
    @Log(title = "开奖设置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(AdminAgentInfo adminAgentInfo)
    {
        List<AdminAgentInfo> list = adminAgentInfoService.selectAdminAgentInfoList(adminAgentInfo);
        ExcelUtil<AdminAgentInfo> util = new ExcelUtil<AdminAgentInfo>(AdminAgentInfo.class);
        return util.exportExcel(list, "agentState");
    }

    /**
     * 获取开奖设置详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:agentState:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(adminAgentInfoService.selectAdminAgentInfoById(id));
    }

    /**
     * 新增开奖设置
     */
    @PreAuthorize("@ss.hasPermi('system:agentState:add')")
    @Log(title = "开奖设置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AdminAgentInfo adminAgentInfo)
    {
        return toAjax(adminAgentInfoService.insertAdminAgentInfo(adminAgentInfo));
    }

    /**
     * 修改开奖设置
     */
    @PreAuthorize("@ss.hasPermi('system:agentState:edit')")
    @Log(title = "开奖设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AdminAgentInfo adminAgentInfo)
    {
        String room = SecurityUtils.getLoginUser().getUser().getRoom();
        if (adminAgentInfo.getAgentState()==2) {
            AdminPrizeRecord prizeRecord = iAdminPrizeRecordService.selectAdminPrizeRecordByPrizeTimeForMax();
            Long period = prizeRecord.getPeriod();
            iAdminUserFlowingService.deleteAdminUserFlowingByRoomAndPeriod(room, period);
            List<AdminUserBetRecord> userBetRecords = iAdminUserBetRecordService.selectAdminUserBetRecordListByRoomAndPeriod(room, period);
            for (AdminUserBetRecord userBetRecord:userBetRecords) {
                AdminWeixinUser user = new AdminWeixinUser();
                user.setWexinId(userBetRecord.getWexinId());
                user.setRoom(room);
                AdminWeixinUser useInfo = iAdminWeixinUserService.selectAdminWeixinUserByWeChatIdAndRoom(user);
                AdminWeixinUser updateUser = new AdminWeixinUser();
                updateUser.setId(useInfo.getId());
                updateUser.setPrice(useInfo.getPrice().add(userBetRecord.getPayPrice()));
                updateUser.setUpdateTime(DateUtils.getNowDate());
                iAdminWeixinUserService.updateAdminWeixinUser(updateUser);
            }
            iAdminUserBetRecordService.deleteAdminUserBetRecordByRoomAndPeriod(room, period);
            String finalSendMsg="停止游戏";
            new Thread(new Runnable() {
                @Override
                public void run() {
                    HttpUtils.sendPost(sendUrl,"sendMsg="+ finalSendMsg+"&room="+room);
                }
            }).start();
        }else{
            String finalSendMsg="开始游戏";
            new Thread(new Runnable() {
                @Override
                public void run() {
                    HttpUtils.sendPost(sendUrl,"sendMsg="+ finalSendMsg+"&room="+room);
                }
            }).start();
        }
        return toAjax(adminAgentInfoService.updateAdminAgentInfo(adminAgentInfo));
    }

    /**
     * 删除开奖设置
     */
    @PreAuthorize("@ss.hasPermi('system:agentState:remove')")
    @Log(title = "开奖设置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(adminAgentInfoService.deleteAdminAgentInfoByIds(ids));
    }
}
